home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 960 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.5 KB  |  131 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: fjh@cs.mu.OZ.AU
  3. Newsgroups: comp.std.c++
  4. Subject: Re: placement new and object reuse
  5. Date: 04 Apr 1996 10:15:25 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <199604040450.21790@munta.cs.mu.OZ.AU>
  9. References: <199604031756.DAA23449@suphys.physics.usyd.edu.au>
  10. Reply-To: c++std-core@research.att.com
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: Thu, 4 Apr 1996 04:50:40 GMT
  13. Errors-To: c++std-postmaster@research.att.com
  14. Precedence: list
  15. Transport-Options: /ignore
  16. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  17.     iQBVAwUBMWQRvky4NqrwXLNJAQFhZwH/UROgKJthWS8rHKhiQL3yDj8pSGFs0s0Y
  18.     N83iWjVKIbM1w/GFv/TPpL3qoKtGx1vypYP4nBEiD9bWZutvakwQIA==
  19.     =8sUI
  20. Originator: austern@isolde.mti.sgi.com
  21.  
  22. To: C++ core language mailing list
  23. Message c++std-core-6512
  24.  
  25. maxtal@Physics.usyd.edu.au (John Max Skaller) writes:
  26.  
  27. >>[Re 3.8/7:]
  28. >>>OK.  On rereading it I can see that the important element is that it is
  29. >>>talking about existing pointers.  Should it not also include references and
  30. >>>if the original was a static or auto, its original 'name'?
  31. >>
  32. >>Yes.  You're right that the wording does not make this clear.
  33. >>It should be clarified.
  34. >
  35. >        What I'd like to know is _what has the storage class
  36. >got to do with it_??
  37.  
  38. It is very simple: only objects with static or auto storage class have names.
  39. We were talking about adding words to 3.8/7 to clarify in the text what is
  40. made clear in the example, that using the original name has the same
  41. effect as using a pointer to the same storage.  These words could just
  42. say "the original name, if any" rather than mentioning the storage class.
  43.  
  44. >        Writing in the store occupied by a const object which
  45. >has not been destroyed may 'cause undefined behaviour' if the
  46. >storage was write protected. This has NOTHING to do with
  47. >the storage class, it is a universal of the memory model.
  48.  
  49. Oh, now you're talking about 3.8/9, not about 3.8/7.
  50. Yes, I agree with what you say, but that guarantee is still not
  51. sufficient to give compilers enough leeway for optimization;
  52. hence we need 3.8/9 as well.
  53.  
  54. >        Furthermore, manually executing the destructor
  55. >of a static or auto const object MUST release any write
  56. >protection immediately PRIOR to the destructor being
  57. >elaborated .. beacuse the destructor is permitted to
  58. >modify the storage.
  59.  
  60. True.  But, consider the following example:
  61.  
  62.     struct foo {
  63.         int member;
  64.         foo(int i) { member = i; }    // inline
  65.         ~foo() { }            // inline
  66.         ...
  67.     };
  68.     extern void bar(const foo &);
  69.     extern void baz();
  70.  
  71.     int main() {
  72.         {
  73.         static const foo x(42);
  74.         bar(x);
  75.         ...
  76.         }
  77.         baz();
  78.     }
  79.  
  80. Can the compiler place `x' in ROM?  The answer is yes only because of 3.8/9.
  81. 3.8/9 gives the compiler writer licence to not turn off write protection
  82. before the destructor if the destructor is known to not write to the object.
  83. Because of 3.8/9, this is OK by the "as if" rule.  But if 3.8/9 didn't
  84. exist, it wouldn't be OK, because some other translation unit could contain
  85.  
  86.     foo *global;
  87.     void bar(const foo &x) {
  88.         global = const_cast<foo *>(&x);
  89.     }
  90.     void baz() {
  91.         new (global) foo(43);    // undefined behaviour by 3.8/9
  92.     }
  93.  
  94. which would mean that the call to baz() would modify the storage for x.
  95.  
  96. The same sort of thing applies to
  97.  
  98.     extern void bar(const int &);
  99.     extern void baz();
  100.  
  101.     int main() {
  102.         { static const int x(42);
  103.           bar(x);
  104.           ...
  105.         }
  106.         baz();
  107.     }
  108.  
  109. Here, you have a static constant int, surely the compiler should be allowed
  110. to put it in ROM!  Fortunately this is true, but only because of 3.8/9.
  111.  
  112. Now, I guess you could argue for consistency that 3.8/9 should apply
  113. to dynamically allocated storage too, but that would prevent any reuse
  114. of storage use for placement new of const objects, which is probably
  115. a bad idea, and there is no similar optimization argument, since
  116. (a) it doesn't happen often enough to be an efficiency concern
  117. and (b) compiler aren't going to be able to optimize such cases anyway.
  118. Hence I think the existing rules are satisfactory.
  119.  
  120. --
  121. Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
  122. WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
  123. PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
  124. ---
  125. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  126.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  127.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  128.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  129.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  130. ]
  131.